package org.testfire.realm.view.json.action;

import java.util.List;
import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.UserTransaction;
import org.apache.log4j.Logger;
import org.json.JSONException;
import org.testfire.realm.control.testfire.TestFireDBAO;
import org.testfire.realm.control.user.UserBean;
import org.testfire.realm.util.MapStoreBean;
import org.testfire.realm.view.json.translator.JsonTranslator;

/*
 * @(#)JsonAction.java
 *
 *
 *
 * ver 1.0 Sep 6, 2007 ggranum
 */

public class CreateJsonStoreAction<T> implements JsonAction<String>
{

    UserBean userBean;

    MapStoreBean mapStoreBean;

    // Inject the lgsrealm entity manager, as defined in the persistence.xml file.
    @PersistenceContext(unitName = "testfire")
    EntityManager em;

    @Resource
    private UserTransaction utx;

    Logger log = Logger.getLogger( CreateJsonStoreAction.class );

    public String perform( String[] argumentKeys, String... args )
    {
        try
        {
            String rowsEl = args[0];
            List rows;
            if ( args.length > 3 )
            {
                for ( int i = 3; i < args.length; i++ )
                {
                    String key = argumentKeys[i];
                    if ( key.startsWith( ":" ) )
                    {
                        key = key.substring( 1 );
                    }
                    if ( key.toLowerCase().endsWith( "id" ) )
                    {
                        try
                        {
                            mapStoreBean.put( key, Integer.valueOf( args[i] ) );
                        }
                        catch ( NumberFormatException e )
                        {
                            log.info( "Could not parse mapStore key to integer: " + key + ":" + args[i] );
                            mapStoreBean.put( key, args[i] );
                        }
                    }
                    else
                    {
                        mapStoreBean.put( key, args[i] );
                    }

                }
            }

            utx.begin();
            rows = (List)TestFireDBAO.getValueExpresionValue( rowsEl );

            String translatorEl = args[1];
            JsonTranslator translator = (JsonTranslator)TestFireDBAO.getValueExpresionValue( translatorEl );
            String fields = args[2];
            utx.commit();
            

            return "{result: " + translator.toJsonArray( rows, fields ) + "}";
        }
        catch ( JSONException e )
        {
            log.error( "Could not create JsonStore", e );
        }
        catch ( Exception e )
        {
            log.error( "Error creating JsonStore", e );
        }
        return "{result: null}";
    }

    public void setUserBean( UserBean userBean )
    {
        this.userBean = userBean;
    }

    public void setMapStoreBean( MapStoreBean mapStoreBean )
    {
        this.mapStoreBean = mapStoreBean;
    }


}